package com.abiao.shop.redis;

import com.abiao.shop.util.TurnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Description: RedisService 封装redis服务
 * @Author: AB
 * @Date: 2021-06-24
 * @Version:v1.0
 */
@Service
public class RedisService {

    @Autowired
    private JedisPool jedisPool;

    /**
     * redisExists 判断一个key是否存在
     * @param key
     * @return
     */
    public <T> Boolean exists(KeyPrefix prefix, String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //根据prefix生成key
            String realKey = prefix.getPrefix()+key;
            return jedis.exists(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * delete 删除一对数据
     * @param key
     * @return
     */
    public <T> Long del(KeyPrefix prefix, String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //根据prefix生成key
            String realKey = prefix.getPrefix()+key;
            return jedis.del(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * redisSet 保存一对 key value
     * @param key
     * @param value
     * @return
     */
    public <T> Boolean set(KeyPrefix prefix, String key, T value){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String str = TurnUtil.beanToString(value);
            if (str==null){
                return false;
            }
            //根据prefix生成key
            String realKey = prefix.getPrefix()+key;
            int seconds = prefix.expireSeconds();
            if (seconds <= 0){
                jedis.set(realKey,str); //永不过期
            }else {
                jedis.setex(realKey,seconds,str);
            }
            return true;
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * redisGet 根据key和class获取存储的value
     * @return
     */
    public <T>  T get(KeyPrefix prefix,String key, Class<T> clazz){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //根据prefix生成key
            String realKey = prefix.getPrefix()+key;
            String str = jedis.get(realKey);
            T t = TurnUtil.stringToBean(str,clazz);
            return t;
        }finally {
            returnToPool(jedis);
        }
    }
    /**
     * redisIncr 将存储的数据值增一，若不是数字类型则改为0再增一
     * @param key
     * @return
     */
    public <T> Long incr(KeyPrefix prefix, String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //根据prefix生成key
            String realKey = prefix.getPrefix()+key;
            return jedis.incr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * redisDecr 将存储的数据值减一，若不是数字类型则改为0再减一
     * @param key
     * @return
     */
    public <T> Long decr(KeyPrefix prefix, String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //根据prefix生成key
            String realKey = prefix.getPrefix()+key;
            return jedis.decr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 将jedis返回连接池
     */
    public void returnToPool(Jedis jedis){
        if (jedis!=null){
            jedis.close();
        }
    }

}
